package com.weixin.xplore.domain;

import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Getter
@Setter
public class Lottery {
    private List<Participant> participants;
    private Random random;

    public Lottery() {
        this.participants = new ArrayList<>();
        this.random = new Random();
    }

    // 添加参与者
    public void addParticipant(Participant participant) {
        participants.add(participant);
    }

    // 抽奖方法
    public Participant drawLottery() {
        if (participants.isEmpty()) {
            return null;
        }

        int totalWeight = 0;
        for (Participant participant : participants) {
            totalWeight += participant.calculateWeight();
        }

        int randomNumber = random.nextInt(totalWeight);
        int currentWeight = 0;
        for (Participant participant : participants) {
            currentWeight += participant.calculateWeight();
            if (randomNumber < currentWeight) {
                return participant;
            }
        }

        return null;
    }
}
